<?php
if (!defined('IN_XLP')) {
    exit('Access Denied');
}
$Document = array(
    'pageid' => 'exchange-edit', //页面标示
    'pagename' => '编辑积分兑换', //当前页面名称
    'mycss' => array(), //加载的css样式表
    'myjs' => array(), //加载的js脚本
	'footerjs'=>array('content/fuelux/fuelux','content/combodate/moment.min','content/combodate/combodate'),
    'head' => true, //加载头部文件
);
include getTpl('header', 'public');
?>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<section id="content">
  <section class="main padder">
    <div class="clearfix">
      <h4><i class="fa fa-edit"></i><?php echo trim($Document['pagename'],'_');?></h4>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <section class="panel">
          <div class="panel-body">
            <form class="form-horizontal" method="post" id="exchangeForm">
              <div class="form-group">
                <label class="col-sm-1 control-label">名称</label>
                <div class="col-sm-3">
                  <input type="text" name="title" id="title" placeholder="" class="form-control" value="<?php echo $rs['title'];?>" />
                  <span class="help-block">标题不能长于80个字符</span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">图片地址</label>
                <div class="col-sm-3">
                  <input type="text" name="img_url" id="img_url" placeholder="" class="form-control" value="<?php echo $rs['img_url'];?>" />
                  <span class="help-block">请上传您的LOGO图标，标准尺寸110px*110px&nbsp; &nbsp; <?php if ($rs['img_url']){?><a rel="pop" href="<?php echo getImgUrl($rs['img_url']);?>">预览>></a><?php }?></span></div>
                <div class="col-sm-3">
                  <iframe width="280" height="24" src="<?php echo U('upload/index',array('id'=>'img_url')); ?>" scrolling="no" frameborder="0"></iframe>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">礼品总数量</label>
                <div class="col-sm-3">
                  <input type="text" name="counts" id="counts" placeholder="" class="form-control" value="<?php echo $rs['counts'];?>" style="width:80px" />
                  <span class="help-block">兑换的上限数量</span>
                  </div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">礼品单位</label>
                <div class="col-sm-3">
                  <input type="text" name="units" id="units" placeholder="" class="form-control" value="<?php echo $rs['units'];?>" style="width:80px" />
                  <span class="help-block">每份礼品的单位，如：份、个、张、提、箱</span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">每份所需积分</label>
                <div class="col-sm-3">
                  <input type="text" name="price" id="price" placeholder="" class="form-control" value="<?php echo $rs['price'];?>"  style="width:80px"/>
                  <span class="help-block">兑换一次需要消耗的积分数，填写纯数字</span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">单用户最多可兑换份数</label>
                <div class="col-sm-3">
                  <input type="text" name="single_counts" id="single_counts" placeholder="" class="form-control" value="<?php echo $rs['single_counts'];?>"  style="width:80px"/>
                  <span class="help-block">单个用户最多可以兑换的数量，0为不限制</span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">开始时间</label>
                <div class="col-sm-3">
                  <input type="text" name="stime" id="stime" placeholder="" class="form-control" value="<?php echo $rs['stime']?outTime($rs['stime']):'';?>" readonly  style="width:222px"/>
                  <span class="help-block"></span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">结束时间</label>
                <div class="col-sm-3">
                  <input type="text" name="etime" id="etime" placeholder="" class="form-control" value="<?php echo $rs['etime']?outTime($rs['etime']):'';?>" readonly  style="width:222px"/>
                  <span class="help-block"></span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">兑换截止时间</label>
                <div class="col-sm-2">
                  <input type="text" name="out_time" id="out_time" placeholder="" class="form-control" value="<?php echo $rs['out_time']?outTime($rs['out_time']):'';?>" readonly  style="width:222px"/>
                  <span class="help-block">用户兑换截止时间，可不填写</span></div>
                  <div class="col-sm-3"><a href="javascript:;" id="clear-time" class="btn btn-default col-md-offset-1">清除</a></div>
              </div>
			<div class="form-group">
                <label class="col-sm-1 control-label">说明事项</label>
                <div class="col-sm-8">
                  <textarea class="form-control" id="content" name="content"  style="height:200px"><?php echo $rs['content'];?></textarea>
                  <span class="help-block"></span></div>
              </div>
              <div class="form-group">
                <label class="col-sm-1 control-label">状态</label>
                <div class="col-sm-5">
                  <div class="row">
                  <?php
                  foreach($status as $key=>$val){
				  ?>
                    <div class="col-xs-2 radio m-l-large">
                      <label class="radio-custom">
                        <input type="radio" name="status"  value="<?php echo $key;?>" <?php if ($key==$rs['status']){echo 'checked="checked"';}?>/>
                        <i class="fa fa-circle-o"></i><?php echo $val;?></label>
                    </div>
				<?php
				  }
				?>
                  </div>
                  <span class="help-block"></span></div>
              </div>
              <div class="form-group">
                <div class="col-sm-9 col-lg-offset-1">
                  <div class="col-sm-1">
                  <input name="id" type="hidden" value="<?php echo $rs['id'];?>" />
                    <button type="submit" id="sub-ok" class="btn btn-primary">保存</button>
                  </div>
                  <div class="col-md-offset-2">
                    <button type="button" class="btn btn-white">取消</button>
                  </div>
                </div>
              </div>
            </form>
          </div>
        </section>
      </div>
    </div>
  </section>
</section>
<!--主体 结束--> 
<link rel="stylesheet" media="screen" href="<?php echo PUBLIC_PATH; ?>jquery-ui/css/smoothness/jquery-ui-1.10.0.custom.min.css"/>
<script type="text/javascript" src="<?php echo PUBLIC_PATH; ?>jquery-ui/js/jquery-ui-1.10.0.custom.min.js"></script>
<script type="text/javascript" src="<?php echo PUBLIC_PATH; ?>jquery-ui/js/jquery-ui-timepicker-addon.js"></script>
<script>
	function checkFormInt(str,name,checkNum){//检查函数
	  //检查是否数字
		if(!(/^\d+$/.test(str))){
			Msg.error(name+"必须为正整数");
			return false;
		}
		if (checkNum&&str<=0){
			Msg.error(name+"必须大于0");
			return false;
		}
		return true;
	}
    $(function() {
		$('#stime').datetimepicker();
        $('#etime').datetimepicker();
		$('#out_time').datetimepicker();
		$("#clear-time").click(function(){
			$('#out_time').val('');	
		})
        $("#exchangeForm").submit(function() {
			if (!$('#title').val()) {
                Msg.error('请输入标题');
                return false;
            }
			if (!$('#img_url').val()) {
                Msg.error('请上传图片');
                return false;
            }
			if (!checkFormInt($('#counts').val(),'礼品总数量',1)||!checkFormInt($('#price').val(),'所需积分数量',1)||!checkFormInt($('#single_counts').val(),'最多可兑换份数')){
				return false;	
			}
			if (!$('#stime').val() || !$('#etime').val()) {
                Msg.error('请输入活动起止时间');
                return false;
            }
			if (!checkTime($("#stime").val(), $("#etime").val())) {
                Msg.error('结束时间必须大于开始时间，且间隔时间大于1分钟');
                return false;
            }
			Msg.loading();
            $.post('<?php echo U('exchange/save');?>', $(this).serialize(), function(result) {
				Msg.hide();
                if (result.status == 1) {
                    Msg.ok('编辑成功',function(){
						window.location.href = '<?php echo U('exchange/index');?>';	
					},1000);
                } else {
                    Msg.error("保存失败");
                }
            }, "json");
            return false;
        })
    })
</script> 
<?php
include getTpl('footer', 'public');
?>